package com.wsc.juc_001.c_008;

/**
 * 模拟银行账户
 * 对业务写方法进行加锁
 * 对业务读方法不进行加锁
 * 这样行不行？
 */
public class Account {
    String name;
    double balance;

    public synchronized void set(String name,Double balance){
        this.name = name;
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.balance = balance;
    }

    public double getBalance(String name){
        return this.balance;
    }

    public static void main(String[] args) {
        Account a =  new Account();
        new Thread(()->{
            a.set("zz",100.0);
        }).start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(a.getBalance("zz"));

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(a.getBalance("zz"));
    }
}
